<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
use Think\Page;
class StaffController extends BaseController{
    public function add(){
        $this->assign('title','添加新员工页面');
        $this->assign('info','添加');
        $this->assign('url',U('Admin/Staff/insert'));
        $this->assign('stationlist',D('Station')->select());
        $this->display('Staff_info');
    }
public function insert(){
       $where['name']=I('post.name');
       $hasname=D('Staff')->where($where)->find();
       if ($hasname){
           $this->error('用户名已存在,添加失败！');
       }else {
           if (D('Staff')->getstaffinfo()){
               $this->success('添加成功',U('Admin/Staff/View'));
           }else {
               $this->error('添加失败');
           }
       }
     
    }

    public function edit(){
        $this->assign('title','员工更新页面');
        $this->assign('info','更新');
        $this->assign('url',U('Admin/Staff/update'));
        $this->assign('editlist',D('Staff')->getid());
        $this->assign('stationlist',D('Station')->select());
        $this->display('Staff_info');
    }
    
    public function update(){
        $updatastaff=D('Staff');
        $result=$updatastaff->updatestaff();
        if(false !== $result || 0 !== $result){
            $this->success('更新成功',U('Admin/Staff/View'));
        }else {
            $this->error('更新失败');
        }
    }
    
    public function delete(){
        if(D('Staff')->delid()){
            $data['state']=1;
            $data['msg']='删除成功';
            $this->ajaxReturn($data);
        }else {
            $data['state']=0;
            $data['msg']='删除失败';
            $this->ajaxReturn($data);
        }
    }
    
    public function view(){        
        $Staffinfo=D('Staff');
        $count=$Staffinfo->count();
        $page=new Page($count, 12);
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->rollPage='10';
        $page->setConfig('theme', '%HEADER%  共    %TOTAL_PAGE% 页   %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show=$page->show();        
        $stafflist=D('Staff')
        ->field("a.*,b.station")
        ->table("__STAFF__ AS a")
        ->join("LEFT JOIN __STATION__ AS b on a.station_id=b.station_id")
        ->limit($page->firstRow.','.$page->listRows)
        ->order('convert(station using utf8) asc')
        ->select();
        $this->assign('title','员工信息列表');
        $this->assign('info','添加员工');
        $this->assign('page',$show);
        $this->assign('stafflist',$stafflist);
        $this->display('Staff_list');
    }
}
